package com.amazon.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.amazon.shop.bean.dto.BaseGroup;
import com.amazon.shop.bean.dto.EmailForm;
import com.amazon.shop.bean.dto.SendEmailForm;
import com.amazon.shop.bean.entity.Emailmodel;
import com.amazon.shop.bean.entity.Evalform;
import com.amazon.shop.bean.entity.User;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.PageVO;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.bean.vo.TblEvalformVO;
import com.amazon.shop.service.IEmailmodelService;
import com.amazon.shop.service.IUserService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 创建时间：19:15 2021/3/10
 *
 * @author lzj
 * @since JDK 1.8.0_171
 * 类说明：
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * --------------------------------------------------------*
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private IEmailmodelService emailService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/send")
    public Ret sendEmail(SendEmailForm sendEmailForm){
        return emailService.send(sendEmailForm);
    }

    @RequestMapping("/get")
    public Ret get(@Validated(value = {BaseGroup.get.class}) EmailForm emailForm){
        String operatorUserid = emailForm.getOperatorUserid();
        Integer ismanager = emailForm.getOismanager();
        Page<Emailmodel> page = new Page(emailForm.getCurrentNum(),emailForm.getPageSize());
        if (StrUtil.isNotBlank(operatorUserid)) {
            emailForm.setUserid(Integer.valueOf(operatorUserid));
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ismanager!=null&&ismanager==0&&emailForm.getUserid()!=null) {
            queryWrapper.eq("userid",emailForm.getUserid());
        }
        page.setOrders(OrderItem.descs("createtime"));
        page = emailService.page(page, queryWrapper);
        PageVO<Object> objectPageVO = new PageVO<>(page);
        return ResultUtil.success(objectPageVO);
    }

    @RequestMapping("/add")
    public Ret add(@Validated(value = {BaseGroup.add.class})EmailForm emailForm){
        Emailmodel emailModel = new Emailmodel();
        BeanUtil.copyProperties(emailForm, emailModel);
        boolean save = emailService.save(emailModel);
        return save?ResultUtil.success():ResultUtil.error(ResultEnum.FAILURE);
    }

    @RequestMapping("/update")
    public Ret update(@Validated(value = {BaseGroup.update.class})EmailForm emailForm){
        UpdateWrapper<Emailmodel> updateWrapper = new UpdateWrapper<>();
        if(StrUtil.isNotBlank(emailForm.getMname())){
            updateWrapper.set("mname",emailForm.getMname());
        }
        if(StrUtil.isNotBlank(emailForm.getContent())){
            updateWrapper.set("content",emailForm.getContent());
        }
        if(emailForm.getIshtml()!=null){
            updateWrapper.set("ishtml",emailForm.getIshtml());
        }
        updateWrapper.set("updatetime",new Date());
        updateWrapper.eq("id",emailForm.getId());
        boolean save = emailService.update(updateWrapper);
        return save?ResultUtil.success():ResultUtil.error(ResultEnum.FAILURE);
    }

    @RequestMapping("/del")
    public Ret del(@Validated(value = {BaseGroup.del.class})EmailForm emailForm){
        boolean b = emailService.removeById(emailForm.getId());
        return b?ResultUtil.success():ResultUtil.error(ResultEnum.FAILURE);
    }
}
